* Firm organization with multiple establishments
* Section III.A: Table III

clear all 
set matsize 2000
set more off, perm

capture log close
log using log/04_facts-cs_MElocation.log, replace

use data/MEloc_2012_cs.dta, clear
desc

label variable ldistall "Log distance to HQ"
label variable ln_MP "Log market potential"
label variable rel_wage "Relative wages"
label variable rel_Preis "Relative land prices"
	
********************************************************************************
***	TABLE III: Location probability - columns 1-3

eststo probit1, title("probit1"): qui probit inv ldistall ln_MP rel_wage d_legal* d_08wz* d_hq_kreis* if hq_kreis != ao_kreis, vce(cluster hq_kreis)
capture drop tag_unt
egen tag_unt = tag(untid) if e(sample) == 1
qui count if tag_unt == 1
qui estadd scalar count_unt = r(N)

eststo probit2, title("probit2"): qui probit inv ldistall ln_MP rel_wage rel_Preis d_legal* d_08wz* d_hq_kreis* if hq_kreis != ao_kreis, vce(cluster hq_kreis)
capture drop tag_unt
egen tag_unt = tag(untid) if e(sample) == 1
qui count if tag_unt == 1
qui estadd scalar count_unt = r(N)

eststo probit3, title("probit3"): qui probit inv ldistall d_ao_kreis* d_legal* d_08wz* d_hq_kreis* if hq_kreis != ao_kreis, vce(cluster hq_kreis)
capture drop tag_unt
egen tag_unt = tag(untid) if e(sample) == 1
qui count if tag_unt == 1
qui estadd scalar count_unt = r(N)

esttab probit1 probit2 probit3, ///
	b(%9.3f) se(%9.3f) star r2 obslast ///
	varwidth(24) stats(N N_clust count_unt, fmt(%9.0g %9.0g %9.0g) label("\# of observations" "\# of HQ counties" "\# of firms")) ///
	compress nogaps mtitles star(+ 0.10 * 0.05 ** 0.01 *** 0.001) style(tex) label sca(N_clust) ///
	indicate("Legal form FE = d_legal*" "HQ sector FE = d_08wz*" "HQ county FE = d_hq_kreis*" "County FE = d_ao_kreis*", labels(Y N) ) 
	capture drop _est_*


********************************************************************************
***	TABLE III: Log # est. employees - columns 4-6

//	Within firm, thus HQ sector, HQ county, legal form FE absorbed

keep if inv == 1
drop inv

xtset, clear
xtset untid ao_kreis

gen ln_empl_kreis = ln(empl_kreis)
label variable ln_empl_kreis "Log # employees of firm in county"

eststo within1, title("within1"): qui xtivreg2 ln_empl_kreis ldistall ln_MP rel_wage if hq_kreis != ao_kreis, fe cluster(untid ao_kreis)
capture drop tag_unt
egen tag_unt = tag(untid) if e(sample) == 1
qui count if tag_unt == 1
qui estadd scalar count_unt = r(N)

eststo within2, title("within2"): qui xtivreg2 ln_empl_kreis ldistall ln_MP rel_wage rel_Preis if hq_kreis != ao_kreis, fe cluster(untid ao_kreis)
capture drop tag_unt
egen tag_unt = tag(untid) if e(sample) == 1
qui count if tag_unt == 1
qui estadd scalar count_unt = r(N)

eststo within3, title("within3"): qui xtivreg2 ln_empl_kreis ldistall d_ao_kreis* if hq_kreis != ao_kreis, fe cluster(untid ao_kreis)
capture drop tag_unt
egen tag_unt = tag(untid) if e(sample) == 1
qui count if tag_unt == 1
qui estadd scalar count_unt = r(N)

esttab within1 within2 within3, ///
	b(%9.3f) se(%9.3f) star r2 obslast ///
	stats(N N_clust1 N_clust2 count_unt, fmt(%9.0f %9.0f %9.0f )) compress nogaps mtitles star(+ 0.10 * 0.05 ** 0.01 *** 0.001) style(tex) label sca(N_clust) ///
	drop(d_ao_kreis*)
	capture drop _est_*

***	TABLE III: Distance to headquarters correlates negatively with location probability and establishment size (complete)
esttab probit1 probit2 probit3 within1 within2 within3, ///
	b(%9.3f) se(%9.3f) star r2 obslast ///
	varwidth(24) stats(N count_unt, fmt(%9.0g %9.0g %9.0g) label("\# of observations" "\# of firms")) ///
	compress nogaps mtitles star(+ 0.10 * 0.05 ** 0.01 *** 0.001) style(tex) label ///
	indicate("Legal form FE = d_legal*" "HQ sector FE = d_08wz*" "HQ county FE = d_hq_kreis*" "County FE = d_ao_kreis*", labels(Y N))
	capture drop _est_*

cap erase data/MEloc_2012_cs.dta

log close
